<?php
declare (strict_types=1);

namespace app\model;

use think\Model;

/**
 * @mixin \think\Model
 */
class Statistics extends AppModel
{
    //

    //微信用户
    public function users(){
        return $this->belongsTo(User::class,'user_id')->field('id,username,name,head_img');
    }

    public function community(){
        return $this->belongsTo(Community::class,'community_id')->field('id,name');
    }

    /**
     * @param $case_id
     * @param $audit_result
     * @User: 刘海龙
     * @Date: 2021/8/10
     * @Time: 10:47
     * @return Statistics|bool|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 处理统计逻辑
     */
    public static function statisticsLogic($case_id, $audit_result)
    {
        $case = ReviewCase::with(['project', 'user'])->find($case_id);

        //查找数据库是否存在统计记录
        $new_data = [];
        $res = self::where(['review_project_id' => $case->review_project_id, 'user_id' => $case->review_people, 'community_id' => $case->user->community_id])->find();

        if (is_null($res)) {
            $new_data['review_project_id'] = $case->review_project_id;
            $new_data['review_project_cycle'] = $case->project->review_cycle;
            $new_data['user_id'] = $case->review_people;
            $new_data['community_id'] = $case->user->community_id;
            if ($audit_result === 1) {
                $new_data['finish'] = 1;
            } else if ($audit_result === 2) {
                $new_data['unfinished'] = 1;
            } else if ($audit_result === 3) {
                $new_data['rebound'] = 1;
            } else {
                $new_data['disqualification'] = 1;
            }
            return self::create($new_data);
        } else {

            //查看原来案件的审核记录是多少

            //获取案件的审核结果 $case->audit_result
            //审核结果是否于案件结果一致
            /**
             * audit_result = 1 2 3 4
             * $case->audit_result = 0 1 2 3 4
             * // 0 第一次审核的
             *  1 不可能存在 已经审核并整改过的
             *  2
             *  3
             *  4
             */

            if ($case->audit_result === ReviewCase::ZERO_AUDIT_RESULT) { // 0
                //暂未审核的  第一次案件审核
                if ($audit_result === 1) {
                    $new_data['finish'] = $res->finish + 1; // +1 递增
                } else if ($audit_result === 2) {
                    $new_data['unfinished'] = $res->unfinished + 1; // +1 递增
                } else if ($audit_result === 3) {
                    $new_data['rebound'] = $res->rebound + 1; // +1 递增
                } else {
                    $new_data['disqualification'] = $res->disqualification + 1; // +1 递增
                }
            } else if ($case->audit_result === ReviewCase::TWO_AUDIT_RESULT) { // 2
                //未整改的
                if ($audit_result === 1) {
                    $new_data['finish'] = $res->finish + 1; //原来记录值+1;
                    $new_data['unfinished'] = $res->unfinished - 1; //原来记录值-1;
                } else if ($audit_result === 2) {
                    $new_data['unfinished'] = $res->unfinished + 1; // /原来记录值+1;
                } else if ($audit_result === 3) {
                    $new_data['rebound'] = $res->rebound + 1; // /原来记录值+1;
                    $new_data['unfinished'] = $res->unfinished - 1; //原来记录值-1;
                } else {
                    $new_data['disqualification'] = $res->disqualification + 1; ///原来记录值+1;
                    $new_data['unfinished'] = $res->unfinished - 1; //原来记录值-1;
                }
            } else if ($case->audit_result === ReviewCase::THREE_AUDIT_RESULT) { // 3
                //反弹的
                if ($audit_result === 1) {
                    $new_data['finish'] = $res->finish + 1; //原来记录值+1;
                    $new_data['rebound'] = $res->rebound - 1; //原来记录值-1;

                } else if ($audit_result === 2) {
                    $new_data['unfinished'] = $res->unfinished + 1; // /原来记录值+1;
                    $new_data['rebound'] = $res->rebound - 1; //原来记录值-1;
                } else if ($audit_result === 3) {
                    $new_data['rebound'] = $res->rebound + 1; // /原来记录值+1;

                } else {
                    $new_data['disqualification'] = $res->disqualification + 1; ///原来记录值+1;
                     $new_data['rebound'] = $res->rebound - 1; //原来记录值-1;
                    ///
                }
            } else if ($case->audit_result === ReviewCase::TOUR_AUDIT_RESULT) { // 4
                //不合格的
                if ($audit_result === 1) {
                    $new_data['finish'] = $res->finish + 1; //原来记录值+1;
                    $new_data['disqualification'] = $res->disqualification - 1; //原来记录值-1;
                } else if ($audit_result === 2) {
                    $new_data['unfinished'] = $res->unfinished + 1; // /原来记录值+1;
                    $new_data['disqualification'] = $res->disqualification - 1; //原来记录值-1;
                } else if ($audit_result === 3) {
                    $new_data['rebound'] = $res->rebound + 1; // /原来记录值+1;
                    $new_data['disqualification'] = $res->disqualification - 1; //原来记录值-1;
                } else {
                    $new_data['disqualification'] = $res->disqualification + 1; ///原来记录值+1;
                }
            }
            return $res->save($new_data);
        }
    }
}
